// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: Wang Shuo wangshuo@sgsimulation.com
 * @Date: 2024-07-17 14:05:21
 * @LastEditors: wxc superjava@qq.com
 * @LastEditTime: 2024-09-29 15:13:26
 * @FilePath: /SGFEM/DBManager/src/Service/Constraint/SPCService.cpp
 */
#include "DBManager/Service/Input/Constraint/SPCService.h"

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <vector>

#include "DataStructure/Common/Id.h"
#include "DataStructure/Common/Out.h"
#include "DataStructure/Common/Status.h"
#include "DataStructure/Input/Constraint/SPCADDData.h"
#include "DataStructure/Input/Constraint/SPCData.h"

#include "DBManager/Common/ServiceMacro.h"
#include "DBManager/IRepository/Input/Constraint/ISPCADDRepository.h"
#include "DBManager/IRepository/Input/Constraint/ISPCRepository.h"
#include "DBManager/Service/Input/Constraint/ConstraintSummaryService.h"

using namespace SG::DBManager;

// 注册服务
SERVICE_REG_IMPL2 (SPCService, ISPCRepository, ISPCADDRepository)

SERVICE_REG_SET_SUMMARY (SPCService, ConstraintSummaryService)
SPCService::SPCService (ISPCRepository& repos, ISPCADDRepository& spcaddRepos) : m_repository (repos), m_spcaddRepository (spcaddRepos)
{}

std::size_t SPCService::Size ()
{
    return m_repository.Size ();
}

SG::DataStructure::Common::Status SPCService::FindByIndex (std::size_t index, SG::DataStructure::FEM::SPCData& _OUT entity)
{
    return m_repository.FindByIndex (index, entity);
}
SG::DataStructure::Common::Status SPCService::FindBySId (const SG::DataStructure::Common::Id                sId,
                                                         std::vector<SG::DataStructure::FEM::SPCData>& _OUT mpcSet)
{
    std::vector<SG::DataStructure::Common::Id> ids;
    // 判断 sid 是否为 mpcadd 数据
    std::vector<SG::DataStructure::FEM::SPCADDData> unionData;

    if (m_spcaddRepository.FindBySId (sId, unionData) == SG::DataStructure::Common::Status::STATUS_Success)
    {
        for (auto&& i : unionData)
        {
            for (auto s : i.m_subSets)
            {
                ids.push_back (s);
            }
        }
    }
    else
    {
        // 不是mpc Add
        ids.push_back (sId);
    }

    // 先清空容器
    mpcSet.clear ();
    SG::DataStructure::FEM::SPCData spcdata;

    for (std::size_t index = 0; index < m_repository.Size (); index++)
    {
        if (m_repository.FindByIndex (index, spcdata) == SG::DataStructure::Common::Status::STATUS_Success)
        {
            if (std::find (ids.begin (), ids.end (), spcdata.m_sId) != ids.end ())
            {
                mpcSet.push_back (spcdata);
            }
        }
    }

    return SG::DataStructure::Common::Status::STATUS_Success;
}
